import request from 'request';

export class HttpUtil {

    static request(options: any = {}) {
        const { body, ...otherOptions } = options;
        const params = {
            url: otherOptions.url,
            method: 'get',
            strictSSL: false,
            headers: {
                ...(otherOptions.headers || {})
            },
            ...otherOptions,
            // form: body && JSON.stringify(body),
            body: body && JSON.stringify(body),
        };
        return new Promise((resolve, reject) => {
            request(params, function (error, response) {
                if (error) {
                    console.log('error------>', error);
                    reject(error);
                    return;
                }
                resolve(response);
            });
        });
    };
    static get(url: string, options = {}) {
        return new Promise((resolve, reject) => {
            request.get(url, options, function (error, response) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(response);
            });
        });
    };
    static post(url: string, options = {}) {
        return new Promise((resolve, reject) => {
            request.post(url, options, function (error, response) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(response);
            });
        });
    };
    static put(url: string, options = {}) {
        return new Promise((resolve, reject) => {
            request.put(url, options, function (error, response) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(response);
            });
        });
    };
    static deleteRequest(url: string, options = {}) {
        return new Promise((resolve, reject) => {
            request.delete(url, options, function (error, response) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(response);
            });
        });
    };

    static patch(url: string, options = {}) {
        return new Promise((resolve, reject) => {
            request.patch(url, options, function (error, response) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(response);
            });
        });
    };
}
